package com.menusifu.onlineorder.menu.demo;


import com.alibaba.fastjson.JSONObject;
import com.menusifu.onlineorder.mongo.dao.SeatingAreasDao;
import com.menusifu.onlineorder.mongo.domain.SeatingAreas;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class MenuDemoController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    SeatingAreasDao seatingAreasDao;

    @RequestMapping("findorder")
    @HystrixCommand(fallbackMethod ="hystrixError" )
    public String findOrder(){

        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        map.add("order","sadfasdf");
        return   restTemplate.postForObject("http://localhost:8080/findAllOrder",map,String.class);

    }

    public String hystrixError(){

        return "sorry server is bussy now! please try leater !";
    }

    @RequestMapping("findOneSeatingAreas")
    public String findOneSeatingAreas(){

        SeatingAreas seatingAreas=new SeatingAreas();
        seatingAreas.setMerchantId("M000004530");
        seatingAreas.setDeleted(true);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withIgnoreCase(true)
                .withIgnorePaths("_class");

        Example<SeatingAreas> example=Example.of(seatingAreas,matcher);
        System.out.println(JSONObject.toJSONString(seatingAreasDao.findOne(example)));
        return "success";
    }


}
